	//
//  ViewController.m
//  DimComDemo
//
//  Created by 于跃隆 on 2019/1/23.
//  Copyright © 2019 于跃隆. All rights reserved.
//

#import "ViewController.h"
#import "ZhuceViewController.h"
#import "AFNetworking.h"
#import "IPonemodel.h"
#import "MainViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic ,weak) UITextField *mima;
@property(nonatomic,strong)UITextField *shoujihao;
@property(nonatomic,strong)NSMutableArray *datascoure;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //数组初始化
    _datascoure=[NSMutableArray new];
    //背景照片
    UIImageView *beijng=[[UIImageView alloc]initWithFrame:self.view.frame];
    beijng.image=[UIImage imageNamed:@"1"];
    [self.view addSubview:beijng];
    //手机的照片
    UIImageView *iphone=[[UIImageView alloc]init];
    //添加到视图上
    [self.view addSubview:iphone];
    //添加图片
    iphone.image=[UIImage imageNamed:@"2"];
    //设置位置和大小
    iphone.sd_layout
    .leftSpaceToView(self.view, 35)
    .topSpaceToView(self.view, 180)
    .widthIs(coordinate_width(30))
    .heightIs(coordinate_height(30));
    NSLog(@"屏幕宽度%f",self.view.width);
    NSLog(@"屏幕高度%f",self.view.height);
    
    //手机的下划线
    UIView *xiahuaxian=[[UIView alloc]init];
    //添加到视图上
    [self.view addSubview:xiahuaxian];
    //设置背景颜色
    xiahuaxian.backgroundColor=[UIColor whiteColor];
    //设置位置和大小
    xiahuaxian.sd_layout
    .leftSpaceToView(self.view, 35)
    .topSpaceToView(self.view, 225)
    .widthIs(330)
    .heightIs(2);
    //手机的
    self.shoujihao=[[UITextField alloc]init];
    //添加到视图上
    [self.view addSubview:self.shoujihao];
    //添加提示文字
    self.shoujihao.placeholder=@"手机号";
    //设置文字大小
    self.shoujihao.font=[UIFont systemFontOfSize:20];
    //设置文字颜色
    self.shoujihao.textColor=[UIColor whiteColor];
    //shoujihao.text=[UIFont systemFontOfSize:10];
    //设置位置和大小
    self.shoujihao.sd_layout
    .leftSpaceToView(self.view, 80)
    .topSpaceToView(self.view, 180)
    .widthIs(240)
    .heightIs(40);
    
    UIImageView *suo=[[UIImageView alloc]init];
    //添加到视图上
    [self.view addSubview:suo];
    //添加图片
    suo.image=[UIImage imageNamed:@"3"];
    //设置位置和大小
    suo.sd_layout
    .leftSpaceToView(self.view, 35)
    .topSpaceToView(self.view, 280)
    .widthIs(30)
    .heightIs(30);
    
    UIView *xiahuaxian1=[[UIView alloc]init];
    //添加到视图上
    [self.view addSubview:xiahuaxian1];
    //设置背景颜色
    xiahuaxian1.backgroundColor=[UIColor whiteColor];
    //设置位置和大小
    xiahuaxian1.sd_layout
    .leftSpaceToView(self.view, 35)
    .topSpaceToView(self.view, 325)
    .widthIs(330)
    .heightIs(2);
    
    
    UIButton *kan = [[UIButton alloc] init];
    UITextField *mima = [[UITextField alloc] init];
    [self.view addSubview:mima];
    self.mima = mima;
    //设置位置和大小
    mima.sd_layout
    .leftSpaceToView(self.view, 80)
    .topSpaceToView(self.view, 280)
    .widthIs(290)
    .heightIs(44);
    //设置位置和大小
    kan.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(40)
    .heightIs(40);
    //设置提示文字
    mima.placeholder=@"手机密码";
    //设置大小
    mima.font = [UIFont systemFontOfSize:20];
    //设置密文
    mima.secureTextEntry = YES;
    //设置右视图
    mima.rightView = kan;
    mima.rightViewMode = UITextFieldViewModeAlways;
    //设置代理对象
    mima.delegate = self;
    
    [kan setImage:[UIImage imageNamed:@"ico_mimabukejian"] forState:UIControlStateNormal];
    [kan setImage:[UIImage imageNamed:@"ico_mimakejian"] forState:UIControlStateHighlighted];
    
    [kan addTarget:self action:@selector(textFieldRightBtnTouchDown:) forControlEvents:UIControlEventTouchDown];
    [kan addTarget:self action:@selector(textFieldRightBtnTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
   
    UIButton *zhuce=[[UIButton alloc]init];
    [self.view addSubview:zhuce];
    [zhuce setTitle:@"快速注册" forState:UIControlStateNormal];
    [zhuce addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
    //设置位置和大小
    zhuce.sd_layout
    .leftSpaceToView(self.view, 270)
    .topSpaceToView(self.view, 340)
    .widthIs(100)
    .heightIs(40);
    
    
 UIButton *  denglu=[[UIButton alloc]init];
    [self.view addSubview:denglu];
    //设置位置和大小
    denglu.sd_layout
    .leftSpaceToView(self.view, 70)
    .topSpaceToView(self.view, 550)
    .widthIs(280)
    .heightIs(40);
    denglu.backgroundColor=[UIColor whiteColor];
    denglu.layer.cornerRadius=8;
    denglu.layer.masksToBounds=YES;
    [denglu setTitle:@"登录" forState:UIControlStateNormal];
    
    
    [denglu setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [denglu addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchDown];
    
}
-(void)denglu{
   
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];
    NSString *str=self.shoujihao.text;
    NSString *strr=self.mima.text;
    NSDictionary *dic=@{@"phone":str,@"pwd":strr};
    
    
    [manager POST:@"http://172.17.8.100/small/user/v1/login" parameters:dic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.datascoure = responseObject[@"result"];
        
        NSLog(@"数据有,%@",self.datascoure);
        if ([responseObject[@"message"] isEqualToString:@"登录成功"]) {
            UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:responseObject[@"message"] preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action  = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                MainViewController *new = [[MainViewController alloc] init];
                [self presentViewController:new animated:YES completion:nil];
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            
        }else{
            UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不正确" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action  = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}
- (void)textFieldRightBtnTouchDown:(UIButton *)rightBtn {
    self.mima.secureTextEntry = NO;
    [self.mima becomeFirstResponder];
   
    self.mima.font = nil;
    self.mima.font = [UIFont systemFontOfSize:20];
    self.mima.textColor=[UIColor whiteColor];
}

- (void)textFieldRightBtnTouchUpInside:(UITextField *)textField {
    self.mima.secureTextEntry = YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.mima endEditing:YES];
    [self.shoujihao endEditing:YES];
}
- (BOOL)textField:(UITextField *)mima shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *allStr = [mima.text stringByReplacingCharactersInRange:range withString:string];
    
    if (mima.isSecureTextEntry == YES) {
        mima.text = allStr;
        return NO;
    }
    
    return YES;
}
-(void)zhuce{
    
    ZhuceViewController *ZC=[[ZhuceViewController alloc]init];
    [self presentViewController:ZC animated:YES completion:nil];
     
     
     
     }
@end
